package com.cobla.struct.stack;

import com.cobla.struct.common.Const;
import lombok.Data;

/**
 * 顺序栈
 */
@Data
public class LinearStack {
    private int[] data;
    private int top;

    public LinearStack() {
        this.data = new int[Const.MAXSIZE];
        this.top = -1;
    }

    /**
     * 栈是否空
     * @return 非空 1，空 0
     */
    public int isEmpty() {
        if (this.top == -1) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 入栈
     * @param x
     * @return
     */
    public int push(int x) {
        if (this.top == Const.MAXSIZE - 1) {
            return 0;
        }
        this.data[++this.top] = x;
        return 1;
    }

    /**
     * 出栈
     * @return 栈为空，返回 null
     */
    public Integer pop() {
        if (this.top == -1) {
            return null;
        } else {
            return this.data[this.top--];
        }
    }


}
